<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\exception\GroupException;
use app\admin\model\SystemMenu;
use app\admin\model\SystemGroup as GroupModel;
use app\admin\validate\GroupValidate;
use app\admin\validate\IDMustBePositiveInt;
use think\facade\View;
use think\Request;

class Group extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $groupModel = GroupModel::getAllGroupInfo();
        View::assign([
            'lists' => $groupModel->toArray()
        ]);
        return View::fetch();
    }

    public function add(Request $request)
    {
        //
        if($request->isPost()){
            (new GroupValidate())->goCheck(GroupValidate::Save);
            $params = $request->param();
            $authority = [];
            if(!empty($params['menus'])){
                $authority = array_keys($params['menus']);
            }
            $params['authority'] = json_encode($authority);
            GroupModel::create($params,['name','status','authority']);
            return json([
                'msg' => '保存成功',
                'error_code' => 0
            ],200);

        }else{
            $menu = SystemMenu::getMenuByAuthority();
            $authority = [1,2,3,4];
            View::assign([
                'menu' => $menu,
                'authority' => $authority
            ]);
            return View::fetch();
        }
    }

    public function edit(Request $request,$id)
    {
        //
        (new IDMustBePositiveInt)->goCheck('check');
        $group = GroupModel::getGroupByID($id);
        if (!$group) {
            throw new GroupException([
                'code' => 404,
                'msg' => '请传入部门ID',
                'errorCode' => 10202
            ]);
        }
        if ($request->isPost()){
            (new GroupValidate())->goCheck(GroupValidate::Update);
            $params = $request->param();
            $authority = [];
            if(!empty($params['menus'])){
                $authority = array_keys($params['menus']);
            }
            $params['authority'] = json_encode($authority);
            $res = $group->save($params);
            if(!$res){
                throw new GroupException([
                    'code' => 404,
                    'msg' => '修改部门失败',
                    'errorCode' => 10203
                ]);
            }
            return json([
                'msg' => '保存成功',
                'error_code' => 0
            ],200);

        }else{
            $menu = SystemMenu::getMenuByAuthority();
            $group = $group->toArray();
            $group['authority'] = empty($group['authority']) ? [] : json_decode($group['authority']);
            View::assign([
                'group' => $group,
                'menu' => $menu
            ]);
            return View::fetch();
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        (new IDMustBePositiveInt)->goCheck('check');
        $group = GroupModel::find($id);
        if (!$group) {
            throw new GroupException([
                'code' => 404,
                'msg' => '请传入部门ID',
                'errorCode' => 10202
            ]);
        } 
        //删除部门时要判断该部门下面是否存在用户

        if(!$group->delete()){
            throw new GroupException([
                'code' => 404,
                'msg' => '删除部门失败',
                'errorCode' => 10204
            ]);
        }
        return json([
            'msg' => '删除成功',
            'error_code' => 0
        ],200);
    }
}
